function ListNode(val, next) {
    this.val = (val === undefined ? 0: val)
    this.next = (next === undefined ? null : next)
}
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
// 迭代
/*
创建两个点 用来进行交换 分别断开相互连接的指针 进行前后交换
* */
var swapPairs = function(head) {
    // 边界条件
    if (head == null || head.next == null) return head
    //创建一个呀哑节点
    let newHead = new ListNode(0)
    let cur = newHead
    newHead.next = head
    while (cur.next != null && cur.next.next != null) {
        let pre = cur.next
        let temp = cur.next.next
        cur.next = temp
        pre.next = temp.next
        temp.next = pre
        cur = pre
    }
    return newHead.next
};

// 递归的解法
var swapPairs = function(head) {
    // 边界条件
    if (head == null || head.next == null) return head
    let temp = head.next
    head.next = swapPairs(temp.next)
    temp.next = head
    return temp
}
let head = new ListNode(1)
let p1 = new ListNode(2)
let p2 = new ListNode(3)
let p3 = new ListNode(4)
head.next = p1;
p1.next = p2;
p2.next = p3;
console.log(swapPairs(head))